package org.itboys.ace.web;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.entity.SysParamTemplate;
import org.itboys.admin.service.SysParamTemplateService;
import org.itboys.admin.tools.AdminPermissionCheck;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.AceJsonPageUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 参数模板
 * 
 * @author huanglei
 *
 */
@RestController
@RequestMapping("/ace/sys/paramTemplate")
public class AceSysParamTemplateController extends BaseController{
	
	@Autowired
	private SysParamTemplateService sysParamTemplateService;
	
	@RequestMapping("/list")
	public ModelAndView paramTemplateList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/sys/paramTemplate-list");
	}
	
	/**
	 * 分页查询列表
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("/paramTemplateList")
	@AdminPermissionCheck("sysParamTemplate:list")
	public void list(HttpServletRequest request, HttpServletResponse response,Model model) {
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		if (null != param.get("app_key")) {
			param.put("app_key", AceUtils.getPattern(param.get("app_key")));
		}
		if (null != param.get("key")) {
			param.put("key", AceUtils.getPattern(param.get("key")));
		}
		Page<SysParamTemplate> page = sysParamTemplateService.pageQuery(param, request);
		AceJsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	/**
	 * 保存
	 * @param SysParamTemplate
	 * @param request
	 * @param response
	 */
	@RequestMapping("/saveParamTemplate")
	public void add(@ModelAttribute SysParamTemplate sysParamTemplate,HttpServletRequest request,HttpServletResponse response) {
		try{
			if(sysParamTemplate==null||sysParamTemplate.getId()==0l){
				sysParamTemplateService.insert(sysParamTemplate);
			}else{
				sysParamTemplateService.update(sysParamTemplate);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}catch(Exception e){
			logger.error("SysParamTemplate save error", e);
			e.printStackTrace();
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public void delete(@PathVariable("id") Long id, HttpServletResponse response){
		try {
			sysParamTemplateService.deleteParamTemplate(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("SysParamTemplate delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/***
	 * 根据id查询字典信息
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/getParamTemplateById/{id}")
	public void getParamTemplateById(@PathVariable("id") Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, sysParamTemplateService.getById(id));
	}
}
